import { $Platform } from "@/utils/const/env";
import { useStorage } from "@/utils/storage";
import { siteinfo } from "@/siteinfo";
export const info = () => uni.getSystemInfoSync();

export const isIos = () => {
	return info().osName === 'ios';
};
export const isAndroid = () => {
	return info().osName === 'android';
};
export const isWechat = () => {
	return info().ua.includes('MicroMessenger');
};
export const platforms = () => {
	let result: string[] = [];
	if (isIos()) {
		result.push($Platform.H5_IOS);
	} else if (isAndroid()) {
		result.push($Platform.H5_ANDROID);
	}
	if (isWechat()) {
		result.push($Platform.H5_WECHAT);
	}
	return result;
};
export const isDev = () => process.env.NODE_ENV !== 'production';
export const isProd = () => process.env.NODE_ENV === 'production';
export const appid = () => {
	return useStorage().get('APPID') || siteinfo.app_id;
}
export const host = () => globalThis.location.origin;
export const name = () => '预约上门';
/**
* 替换地址栏#参数不跳转
* @param key 参数名
* @param value 参数值
* @returns void
* @example replaceUrlParam('key','value')
*/
export const replaceUrlParam = (key: string, value: string) => {
	const url = new URL(globalThis.location.href);
	const queryArr = url.hash.split('?');
	const hashParams = new URLSearchParams(queryArr[1] ?? ''); // 获取hash中的参数
	// 设置新的参数值
	hashParams.set(key, value);
	// 更新hash
	url.hash = queryArr[0] + '?' + hashParams.toString();
	globalThis.history.replaceState({}, '', url.href);
}
/**
 * 批量替换地址栏#参数不跳转
 * @param params 参数对象
 * @returns void
 * @example replaceUrlParams({key1:'value1',key2:'value2'})
 */
export const replaceUrlParams = (params: any) => {
	const url = new URL(globalThis.location.href);
	const queryArr = url.hash.split('?');
	const hashParams = new URLSearchParams(queryArr[1] ?? ''); // 获取hash中的参数
	// 设置新的参数值
	for (let x in params) {
		hashParams.set(x, params[x]);
	}
	// 更新hash
	url.hash = queryArr[0] + '?' + hashParams.toString();
	globalThis.history.replaceState({}, '', url.href);
}
export const chooseImage=(options: UniNamespace.ChooseImageOptions)=>{
	uni.chooseImage({
		count: 1,
		sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
		sourceType: ['album', 'camera'], //从相册选择
		...options
	});
}
export const chooseVideo=(options: UniNamespace.ChooseVideoOptions)=>{
	uni.chooseVideo({
		sourceType: ['album', 'camera'], //从相册选择
		...options
	});
}
export const saveImageToPhotosAlbum = (options: UniNamespace.SaveImageToPhotosAlbumOptions) => {
	const aEl=document.createElement('a');
	aEl.href=options.filePath;
	aEl.download='download';
	aEl.click();
	options.success?.({
		path:''
	});
}